﻿using System;
using System.Text;
using HslCommunication.Core.Net;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// UDP客户端的类，负责发送数据到服务器，然后从服务器接收对应的数据信息，该数据经过HSL封装<br />
	/// UDP client class, responsible for sending data to the server, and then receiving the corresponding data information from the server, the data is encapsulated by HSL
	/// </summary>
	// Token: 0x02000101 RID: 257
	public class NetUdpClient : NetworkUdpBase
	{
		/// <summary>
		/// 实例化对象，指定发送的服务器地址和端口号<br />
		/// Instantiated object, specifying the server address and port number to send
		/// </summary>
		/// <param name="ipAddress">服务器的Ip地址</param>
		/// <param name="port">端口号</param>
		// Token: 0x06001568 RID: 5480 RVA: 0x0006F103 File Offset: 0x0006D303
		public NetUdpClient(string ipAddress, int port)
		{
			this.IpAddress = ipAddress;
			this.Port = port;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字符串数据，忽略了自定义消息反馈<br />
		/// The client makes a request to the server, requesting string data, and ignoring custom message feedback
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001569 RID: 5481 RVA: 0x0006F120 File Offset: 0x0006D320
		public OperateResult<string> ReadFromServer(NetHandle customer, string send = null)
		{
			OperateResult<byte[]> operateResult = this.ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
			bool flag = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<string>(Encoding.Unicode.GetString(operateResult.Content));
			}
			return result;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字节数据<br />
		/// The client makes a request to the server, requesting byte data
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送的字节内容</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x0600156A RID: 5482 RVA: 0x0006F176 File Offset: 0x0006D376
		public OperateResult<byte[]> ReadFromServer(NetHandle customer, byte[] send)
		{
			return this.ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字符串数据，并返回状态信息<br />
		/// The client makes a request to the server, requests string data, and returns status information
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x0600156B RID: 5483 RVA: 0x0006F190 File Offset: 0x0006D390
		public OperateResult<NetHandle, string> ReadCustomerFromServer(NetHandle customer, string send = null)
		{
			OperateResult<NetHandle, byte[]> operateResult = this.ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
			bool flag = !operateResult.IsSuccess;
			OperateResult<NetHandle, string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<NetHandle, string>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<NetHandle, string>(operateResult.Content1, Encoding.Unicode.GetString(operateResult.Content2));
			}
			return result;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字节数据，并返回状态信息<br />
		/// The client makes a request to the server, requests byte data, and returns status information
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x0600156C RID: 5484 RVA: 0x0006F1EC File Offset: 0x0006D3EC
		public OperateResult<NetHandle, byte[]> ReadCustomerFromServer(NetHandle customer, byte[] send)
		{
			return this.ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		}

		/// <summary>
		/// 发送的底层数据，然后返回结果数据<br />
		/// Send the underlying data and then return the result data
		/// </summary>
		/// <param name="send">需要发送的底层数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x0600156D RID: 5485 RVA: 0x0006F208 File Offset: 0x0006D408
		private OperateResult<byte[]> ReadFromServerBase(byte[] send)
		{
			OperateResult<NetHandle, byte[]> operateResult = this.ReadCustomerFromServerBase(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<byte[]>(operateResult.Content2);
			}
			return result;
		}

		/// <summary>
		/// 发送的底层数据，然后返回结果数据，该结果是带Handle信息的。<br />
		/// Send the underlying data, and then return the result data, the result is with Handle information.
		/// </summary>
		/// <param name="send">需要发送的底层数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x0600156E RID: 5486 RVA: 0x0006F244 File Offset: 0x0006D444
		private OperateResult<NetHandle, byte[]> ReadCustomerFromServerBase(byte[] send)
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<NetHandle, byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<NetHandle, byte[]>(operateResult);
			}
			else
			{
				result = HslProtocol.ExtractHslData(operateResult.Content);
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x0600156F RID: 5487 RVA: 0x0006F27F File Offset: 0x0006D47F
		public override string ToString()
		{
			return string.Format("NetUdpClient[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
